<template>
  <div>
    <el-dialog title="角色分配" :visible="isShowRole" @close="btnCancel">
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in list " :key="item.id" :label="item.id">
          {{
            item.name
          }}
        </el-checkbox>
      </el-checkbox-group>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting.js'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    isShowRole: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      // 被选中的集合
      roleIds: [],
      list: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取所有角色
    async getRoleList() {
      const { rows } = await getRoleList()
      this.list = rows
    },
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.roleIds = roleIds // 赋值本用户的角色
    },
    async btnOK() {
      // 发请求
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$message.success('操作成功')
      this.$emit('update:isShowRole', false)
    },
    btnCancel() {
      this.roleIds = []
      this.$emit('update:isShowRole', false)
    }
  }
}
</script>

<style>

</style>
